---
description: Hasura Cloud Query tags
sidebar_label: Query Tags
title: 'Cloud & Enterprise: Query Tags'
keywords:
  - hasura
  - docs
  - cloud
  - query-tags
  - monitoring
sidebar_position: 6
sidebar_class_name: cloud-and-enterprise-icon
---

import HeadingIcon from '@site/src/components/HeadingIcon';
import ProductBadge from '@site/src/components/ProductBadge';

# Query Tags

<ProductBadge pro ee self />

## Introduction

Query Tags are SQL comments which are made up of `(key=value)` pairs that are appended to the SQL statements generated
by Hasura for GraphQL operations. This enables the ability to get some application context in the database logs and also
use native database monitoring tools (_e.g. pganalyze_) for better performance analysis.

Example:

When the following query is sent to Hasura

```graphql
query GetChild {
  child {
    name
  }
}
```

Hasura attaches query tags (_unless disabled_) to the generated SQL statement it sends to the database.

```sql
SELECT name FROM child /* request_id=487c2ed5-08a4-429a-b0e0-4666a82e3cc6, field_name=child, operation_name=GetChild */
```

:::warning Query tags are not compatible with all services

As an example,
[Google Cloud SQL](https://cloud.google.com/sql/docs/postgres/using-query-insights#adding-tags-to-sql-queries) drops all
keys not in their prescribed list.

:::

## Formats of Query Tags

The format of the Query Tags describes how the <em>(Key,Value)</em> pairs are constructed. Currently we support three
formats of Query Tags:

1.  Standard (`standard`)
2.  Standard, but prepended to the query (`standard_prepended`)
3.  SQLCommenter (`sqlcommenter`)

### Standard (and `standard_prepended`)

This format makes a collection of <em>key=value</em> pairs and separates each pair by a comma `,` . **This is the
default format for Query Tags**

For eg:

```sql
SELECT name FROM child /* request_id=487c2ed5-08a4-429a-b0e0-4666a82e3cc6, field_name=child, parameterized_query_hash=b2a71ce23928ca7f0021f9060e5d590e9f9bb00f, operation_name=GetChild */
```

`standard_prepended` is the same as `standard` shown above, but the comment will be added _before_ the query, rather
than at the end

### SQLCommenter

The specification for this format is defined at <https://google.github.io/sqlcommenter/spec/>

For eg:

```sql
SELECT name FROM child /* field_name='child', operation_name='GetChild', parameterized_query_hash='b2a71ce23928ca7f0021f9060e5d590e9f9bb00f' ,  request_id='487c2ed5-08a4-429a-b0e0-4666a82e3cc6' */
```

## Information in Query Tags

The following information is present in query tags for the GraphQL operations.

### Query and Mutation

1.  Request ID (`request_id`) - can be added by setting the `omit_request_id` flag to `false`
2.  Operation Name (`operation_name`)
3.  (Root) field name (alias if provided) (`field_name`)
4.  Parameterized Query Hash (`parameterized_query_hash`)

### Subscriptions

1.  (Root) field name (alias if provided) (`field_name`)
2.  Parameterized Query Hash (`parameterized_query_hash`)

## Metadata Specification

```yaml
sources:
  name: # Name of the source
  configuration:
  query_tags: # Optional Field
    disabled: # Optional Field | Type: Bool | Values: true or false
    format: # Optional Field  | Values: standard or sqlcommenter
    omit_request_id: # Optional Field | Type: Bool | Values: true or false
```

:::info

Note The default format for the Query Tags is `Standard` and it is enabled by default for all sources.

:::

In the above Metadata spec:

1.  The <em>query_tags</em> field is optional. If the <em>query_tags</em> field is not present for a source, then query
    tags is enabled for the source and the format used is <em>standard</em>.
2.  To disable query tags for any source, set the value of <em>disabled</em> field to <em>true</em>.
3.  To override the default format (<em>Standard</em>) for query tags, use the <em>format</em> field.
4.  To add the `request_id` part of the query tags for any source, set the value of <em>omit_request_id</em> field to
    <em>false</em>.

:::info Compatibility with prepared statements

The `use_prepared_statements` flag supported by Postgres sources is largely incompatible with query tags. With query
tags enabled, two otherwise identical GraphQL queries may produce different SQL text, which negates the caching benefit
of prepared statements. If both query tags and `use_prepared_statements` are enabled at the same time, the
`omit_request_id` should be set to `true`.

:::

## Example Metadata Specification

```yaml
sources:
  - name: test_db
    configuration:
    query_tags:
      disabled: true

  - name: hasura_db_herokou
      configuration:
      query_tags:
        format: sqlcommenter
        omit_request_id: false

  - name: hasura_db_2
      configuration:
      query_tags:
        format: standard
        disabled: true
```

## Metadata API To Set Query Tags

```yaml
type: 'set_query_tags'
args:
  source_name: # Name of the source | Required
  disabled: # Optional Field | Type: Bool | Values: true or false
  format: # Optional Field  | Values: standard or sqlcommenter
  omit_request_id: # Optional Field | Type: Bool | Values: true or false
```
